<script setup lang="ts">
import type { Video } from '~/video';
// useFetch获取接口数据
const { data: channelList } = await useFetch("/api/channel");
const { data: video } = await useFetch("/api/video");
// 添加请求头
useHead({
  meta: [{ name: "referrer", content: "no-referrer" }],
});

// 显示的列表
const list = ref<Video[]>([]);
// 加载状态
const loading = ref(false);
// 是否完成
const finished = ref(false);
// 页码
let page = 1
let pageSize = 20
// 触底触发的函数
const onLoad = () => {
  finished.value = false
  // 根据当前页面提取数据
  const data = video.value?.slice((page-1)*pageSize,page*pageSize) as Video[]
  // 追加数据
  list.value.push(...data)
  page++
  if(video.value?.length === list.value.length){
    finished.value = true
  }
};

// 主动渲染前20条数据，用于首屏渲染，方便seo抓取数据
onLoad()
</script>

<template>
  <AppHeader></AppHeader>
  <!-- 频道模块 -->
  <van-tabs>
    <van-tab v-for="item in channelList" :key="item.id" :title="item.name" />
  </van-tabs>
  <!-- 视频列表 -->
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <div class="video-list">
      <AppVideo v-for="item in list" :key="item.aid" :item="item"></AppVideo>
    </div>
  </van-list>
</template>

<style lang="scss">
@import "./assets/styles/base.scss";
// 字体图标
@import "./assets/styles/iconfont.scss";
// 公共头部
.app-header {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background-color: #fff;
  .logo {
    flex: 1;
    .Navbar_logo {
      color: #fb7299;
      font-size: 28px;
    }
  }
  .search {
    padding: 0 8px;
    .ic_search_tab {
      color: #ccc;
      font-size: 22px;
    }
  }
  .face {
    padding: 0 15px;
    img {
      width: 24px;
      height: 24px;
    }
  }
  .down-app {
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fb7299;
    color: #fff;
    border-radius: 5px;
    padding: 5px 10px;
  }
}

// 视频列表
.video-list {
  display: flex;
  flex-wrap: wrap;
  padding: 10px 5px;
}

// 视频卡片
.v-card {
  width: 50%;
  padding: 0 5px 10px;
  .card {
    position: relative;
    background: #f3f3f3 url(@/assets/images/default.png) center no-repeat;
    background-size: 36%;
    border-radius: 2px;
    overflow: hidden;
    .card-img {
      .pic {
        height: 100px;
        width: 100%;
        object-fit: cover;
      }
    }
    .count {
      background-image: linear-gradient(0deg, #000000d9, #0000);
      color: #fff;
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      font-size: 12px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 5px 6px;
      span {
        .iconfont {
          font-size: 12px;
        }
      }
    }
  }
  .title {
    margin-top: 5px;
    font-size: 12px;
    color: #212121;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}
</style>